﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="CreateJobCommand.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace HpcSchedulerManagement.Commands
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Common;

    using DataContracts;

    using RestClient;

    public class CreateJobCommand : HpcSchedulerCommandBase
    {
        private readonly string hpcClusterName;

        private readonly IDictionary<string, object> properties;

        private SchedulerJob job;

        public CreateJobCommand(IHpcSchedulerServiceContext serviceContext, string hpcClusterName, IDictionary<string, object> properties)
            : base(serviceContext)
        {
            if (string.IsNullOrEmpty(hpcClusterName))
            {
                throw new ArgumentException("Hpc cluster name must not be null or empty.", "hpcClusterName");
            }

            if (properties == null || !properties.Any())
            {
                throw new ArgumentException("Properties must not be null or empty.", "properties");
            }

            this.hpcClusterName = hpcClusterName;
            this.properties = properties;
        }

        public CreateJobCommand(IHpcSchedulerServiceContext serviceContext, string hpcClusterName, SchedulerJob job)
            : base(serviceContext)
        {
            if (string.IsNullOrEmpty(hpcClusterName))
            {
                throw new ArgumentException("Hpc cluster name must not be null or empty.", "hpcClusterName");
            }

            if (job == null)
            {
                throw new ArgumentNullException("job", "Job must not be null.");
            }

            this.hpcClusterName = hpcClusterName;
            this.job = job;
        }

        protected override RequestContext GetRequestContext()
        {
            return this.CreatePostOperationRequestContext();
        }

        protected override string FormatPath(IHpcSchedulerServiceContext serviceContext)
        {
            return string.Format("{0}/Jobs", this.hpcClusterName);
        }

        protected override string FormatBody()
        {
            if (this.properties != null)
            {
                return XmlHelper.Serialize(
                            new ArrayOfProperty
                                {
                                    Property = this.properties.Select(x => new Property(x.Key, x.Value.ToString())).ToList()
                                });
            }

            return XmlHelper.Serialize(HpcDataContractsTranslator.JobToArrayOfProperties(this.job));
        }
    }
}